#include <stdio.h>
#include <stdlib.h>
#include "LinkList.h"
List *Dlist_Init() // 带头节点的链表初始化
{
    List *list = (List *)malloc(sizeof(List));
    list->first = NULL;
    list->tail = NULL;
    list->now = NULL; // 当前节点
    list->lenth = 0;
    return list;
}
int Dlist_Print(List *list) // 遍历链表
{
    DNODE *pnow = list->first;
    if (list->lenth == 0)
    {
        return 0;
    }
    else
    {
        while (pnow)
        {
            printf("%d ", pnow->data);
            pnow = pnow->next;
        }
        printf("链表长度为：%d\n", list->lenth);
    }
    return 1;
}
int Dlist_InsHead(List *list, LElemType data) // 头插
{
    if (list == NULL)
    {
        return 0;
    }
    DNODE *pnow = list->first;

    DNODE *pnew = (DNODE *)malloc(sizeof(DNODE));
    pnew->data = data;
    pnew->next = NULL;
    pnew->prev = NULL;
    if (list->first == NULL)
    {
        list->first = pnew;
        list->tail = pnew;
        list->lenth++;
    }
    else
    {
        pnew->next = pnow;
        pnow->prev = pnew;
        list->lenth++;
        list->first = pnew;
    }
    list->now = pnew; // 当前节点指向头节点
    return 1;
}
int Dlist_InsTail(List *list, LElemType data) // 尾插
{
    if (list == NULL)
    {

        return 0;
    }
    DNODE *pnow = list->tail;
    DNODE *pnew = (DNODE *)malloc(sizeof(DNODE));
    pnew->data = data;
    pnew->next = NULL;
    pnew->prev = NULL;
    if (list->first == NULL)
    {
        list->first = pnew;
        list->tail = pnew;
        list->lenth++;
    }
    else
    {
        pnew->prev = pnow;
        pnow->next = pnew;
        list->lenth++;
        list->tail = pnew;
    }
    list->now = pnew; // 当前节点指向尾节点
    return 1;
}
DNODE *Dlist_DeleForNode(List *list, DNODE *pdel) // 按地址删除指定节点、返回下一个节点
{
    if (list == NULL)
    {

        return NULL;
    }
    DNODE *pnow = NULL;
    if (list->lenth == 0)
    {
        return NULL;
    }
    if (list->lenth == 1)
    {
        // 只有一个元素
        list->first = NULL;
        list->tail = NULL;
        free(pdel);
        list->lenth--;
        list->now = NULL;
        return NULL;
    }
    else if (pdel->prev == NULL)
    {
        // 删除头
        pnow = pdel->next;
        pdel->next->prev = NULL;
        list->first = pdel->next;
        free(pdel);
        list->lenth--;
        list->now = pnow;
        return pnow;
    }
    else if (pdel->next == NULL)
    {
        // 删除尾
        pdel->prev->next = NULL;
        list->tail = pdel->prev;
        free(pdel);
        list->lenth--;
        list->now = list->tail;
        return NULL;
    }
    else
    {
        // 删除中间
        pnow = pdel->next;
        pdel->prev->next = pdel->next;
        pdel->next->prev = pdel->prev;
        free(pdel);
        list->lenth--;
        list->now = pnow;
        return pnow;
    }
}
int Dlist_DeleForData(List *list, LElemType data) // 删除指定数据
{
    if (list == NULL)
    {
        return 0;
    }
    DNODE *pnow = list->first;
    while (pnow)
    {
        if (pnow->data == data)
        {
            pnow = Dlist_DeleForNode(list, pnow);
        }
        else
            pnow = pnow->next;
    }
    return 1;
}
int Dlist_GetHeadData(List *list, LElemType *data) // 获取头节点
{
    if (!list)
    {

        return 0;
    }
    if (list->lenth == 0)
    {

        return 0;
    }
    *data = list->first->data; // 获取头节点数据
    return 1;
}
int Dlist_GetTailData(List *list, LElemType *data) // 获取尾节点
{
    if (!list)
    {
        return 0;
    }
    if (list->lenth == 0)
    {
        return 0;
    }
    *data = list->tail->data; // 获取尾节点数据
    return 1;
}
int Dlist_IsEmpty(List *list) // 判断链表是否为空
{
    if (list->lenth == 0)
    {
        return 1;
        // printf("链表为空\n");
    }
    else
    {
        return 0;
        // printf("链表不为空\n");
    }
}
int Dlist_Clear(List *list) // 清空列表
{
    if (list == NULL)
    {
        return 0;
    }
    while (list->lenth != 0)
    {
        Dlist_DeleForNode(list, list->first);
    }
    return 1;
}
List *Dlist_Destory(List *list) // 销毁链表
{
    Dlist_Clear(list);
    free(list);
    return NULL;
}
int Dlist_InsUp(List *list, LElemType data) // 从小到大插入
{
    if (list == NULL)
    {
        return 0;
    }
    DNODE *pnow = list->first;
    DNODE *pnew = (DNODE *)malloc(sizeof(DNODE));
    pnew->data = data;
    pnew->next = NULL;
    pnew->prev = NULL;
    if (list->first == NULL)
    {
        // 链表为空，插入第一个元素
        list->first = pnew;
        list->tail = pnew;
        list->lenth++;
        list->now = pnew;
        return 1;
    }
    while (pnow)
    {
        if (pnow->data > data)
        {
            break;
        }
        pnow = pnow->next;
    }

    if (pnow == NULL)
    { // 插入链表尾部
        pnew->prev = list->tail;
        list->tail->next = pnew;
        list->tail = pnew;
        list->lenth++;
        list->now = pnew;
        return 1;
    }
    else if (pnow == list->first)
    { // 插入链表头部
        pnew->next = list->first;
        list->first->prev = pnew;
        list->first = pnew;
        list->lenth++;
        list->now = pnew;
        return 1;
    }
    else
    {
        // 插入链表中间
        pnew->prev = pnow->prev;
        pnew->next = pnow;
        pnow->prev->next = pnew;
        pnow->prev = pnew;
        list->lenth++;
        list->now = pnew;
        return 1;
    }
}
int Dlist_GetNowData(List *list, LElemType *data)// 获取当前节点数据
{
    if (list->now == NULL)
    {
        // printf("当前节点为空\n");
        return 0;
    }
    *data = list->now->data;
    return 1;
}
